package com.syezon.wifikey.speedup;

import com.blankj.utilcode.util.SPUtils;
import com.syezon.wifikey.BuildConfig;
import com.syezon.wifikey.utils.DateUtils;

import java.util.Date;
import java.util.Random;

public class SpeedUpConfig {

    private static final String CONFIG_NAME = "speed_up_config";
    private static final String TODAY_SPEED_UP_COUNT = "today_speed_up_count";
    private static final String CURRENT_SCORE = "current_score";

    public static int getCurrentScore(String ssid) {
        try {
            int currentDayInt = DateUtils.getDayInt(new Date());
            Random random = new Random();
            int initScore = 70 + random.nextInt(10);
            int todaySpeedUpCount = getTodaySpeedUpCount(ssid);
            if (todaySpeedUpCount <= 0) {
                saveCurrentScore(ssid, initScore);
                return initScore;
            } else {
                String todayScore = SPUtils.getInstance(CONFIG_NAME).getString(CURRENT_SCORE + "_" + ssid, "" + currentDayInt + "_" + initScore);
                if (todayScore != null && todayScore.split("_") != null) {
                    String[] dayScoreArray = todayScore.split("_");
                    if (dayScoreArray.length == 2) {
                        String date = dayScoreArray[0];
                        String addCountStr = dayScoreArray[1];
                        int speedUpCount = 0;
                        if (date.equals("" + currentDayInt)) {
                            int addFreeCount = Integer.parseInt(addCountStr);
                            speedUpCount = addFreeCount;
                        } else {
                            speedUpCount = initScore;
                        }
                        return speedUpCount;
                    }
                }
            }
        } catch (Exception e) {

        }
        return 0;
    }

    public static void saveCurrentScore(String ssid, int score) {
        try {
            int currentDayInt = DateUtils.getDayInt(new Date());
            SPUtils.getInstance(CONFIG_NAME).put(CURRENT_SCORE + "_" + ssid, "" + currentDayInt + "_" + score);
        } catch (Exception e) {

        }
    }

    public static int getCurrentAddPercent(String ssid) {
        int todaySpeedUpCount = getTodaySpeedUpCount(ssid);
        Random random = new Random();
        if (todaySpeedUpCount == 0) {
            return 10;
        } else {
            return 2 + random.nextInt(2);
        }
    }

    public static int getTodaySpeedUpCount(String ssid) {
        try {
            int currentDayInt = DateUtils.getDayInt(new Date());
            String todayAddCount = SPUtils.getInstance(CONFIG_NAME).getString(TODAY_SPEED_UP_COUNT + "_" + ssid, "" + currentDayInt + "_" + 0);
            if (todayAddCount != null && todayAddCount.split("_") != null) {
                String[] dayAddCountArray = todayAddCount.split("_");
                if (dayAddCountArray.length == 2) {
                    String date = dayAddCountArray[0];
                    String addCountStr = dayAddCountArray[1];
                    int speedUpCount = 0;
                    if (date.equals("" + currentDayInt)) {
                        int addFreeCount = Integer.parseInt(addCountStr);
                        speedUpCount = addFreeCount;
                    } else {
                        speedUpCount = 0;
                    }
                    return speedUpCount;
                }
            }
        } catch (Exception e) {

        }
        return 0;
    }

    public static final void addSpeedUpCount(String ssid) {
        try {
            int currentDayInt = DateUtils.getDayInt(new Date());
            String todayAddCount = SPUtils.getInstance(CONFIG_NAME).getString(TODAY_SPEED_UP_COUNT + "_" + ssid, "" + currentDayInt + "_" + 0);
            if (todayAddCount != null && todayAddCount.split("_") != null) {
                String[] dayAddCountArray = todayAddCount.split("_");
                if (dayAddCountArray.length == 2) {
                    String date = dayAddCountArray[0];
                    String addCountStr = dayAddCountArray[1];
                    int addCount = 0;
                    if (date.equals("" + currentDayInt)) {
                        int addFreeCount = Integer.parseInt(addCountStr);
                        addCount = addFreeCount + 1;
                    } else {
                        addCount = addCount + 1;
                    }
                    SPUtils.getInstance(CONFIG_NAME).put(TODAY_SPEED_UP_COUNT + "_" + ssid, "" + currentDayInt + "_" + addCount);
                }
            }
        } catch (Exception e) {

        }
    }

}
